﻿using System.Windows.Forms;
using System;
using WorkFlowBLL;
using EntityBLL;
using Utils;
using Library;
using System.Text;
using System.Collections.Specialized;

namespace QLCF.Forms
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtUserName.Focus();
        }

        private void btnLogin_Click(object sender, System.EventArgs e)
        {
            String strUserName = this.txtUserName.Text.Trim().Replace("'", "''");
            String strPassword = this.txtPass.Text.Trim().Replace("'", "''");

            //Check đăng nhập
            if (txtUserName.Text.Trim() == "")
            {
                MessageBox.Show("Bạn chưa nhập tên đăng nhập!", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtUserName.Focus();
                return;
            }
            if (txtPass.Text.Trim() == "")
            {
                MessageBox.Show("Bạn chưa nhập mật khẩu!", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtPass.Focus();
                return;
            }

            CtrAdmin ctrUser = new CtrAdmin();
            var objCheck = ctrUser.CheckExistUser(strUserName, strPassword);
            if (objCheck)
            {                
                Program.isLogin = true;                

                var objUserInfo = ctrUser.AdminGetInfoByUserName(strUserName);
                

                if (objUserInfo != null)
                {
                    if ((objUserInfo.UserName != "admin") && (ctrUser.GetListRoleInAdmin(objUserInfo.AdminID).Items.Count <= 0))
                    {
                        MessageBox.Show("Bạn chưa được cấp quyền vào hệ thống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                    else
                    {

                        Program.UserEntity = new User();
                        if (strUserName != "admin")
                        {

                            LoginBackendAdmin(strUserName, false);

                        }
                        else
                        {
                            LoginBackendAdmin(strUserName, true);
                        }

                        Program.UserEntity.UserName = strUserName;
                        Program.UserEntity.FullName = objUserInfo.FullName;
                        Program.UserEntity.UserID = objUserInfo.AdminID;
                        this.Hide();
                        frmMain frmMain = new frmMain();
                        frmMain.Show();                        
                        frmMain.txtUserLogin.Text = "Đăng nhập: " + Program.UserEntity.FullName + " (" + Program.UserEntity.UserName + ")";
                    }
                }
            }
            else
            {
                MessageBox.Show("Chưa nhập đúng tên đăng nhập hoặc mật khẩu!", "Thông báo...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtUserName.Focus();
                return;
            }
        }

        private void button2_Click(object sender, System.EventArgs e)
        {
            Application.ExitThread();
        }
        

        public void LoginBackendAdmin(string userName, bool isRoot)
        {
            CtrAdmin ctrAdmin = new CtrAdmin();
            StringBuilder permission = new StringBuilder();
            permission.Append('|');
            HybridDictionary hbFunction = new HybridDictionary();
            int _adminID = ctrAdmin.GetInfoAdminByUserName(userName).AdminID;
            int permissionItem;
            if (isRoot)
            {
                var tp = ctrAdmin.GetListFunctionByAdminRootForLogin();
                foreach (DataContext.uspFunctionGetListByAdminRootForLoginResult Function in tp)
                {
                    if (hbFunction[Function.URL] == null)
                    {
                        hbFunction.Add(Function.URL, Function);
                    }

                }
                DataContext.uspFunctionGetListByAdminRootForLoginResult functionInfo;
                foreach (string key in hbFunction.Keys)
                {
                    functionInfo = hbFunction[key] as DataContext.uspFunctionGetListByAdminRootForLoginResult;
                    permission.Append(functionInfo.URL).Append("#").Append("15").Append("|");
                }
            }
            else
            {
                DataContext.uspFunctionGetListByAdminIDForLoginResult functionInfo;
                var tp = ctrAdmin.GetListFunctionByAdminIDForLogin(_adminID);
                foreach (DataContext.uspFunctionGetListByAdminIDForLoginResult Function in tp)
                {
                    if (hbFunction[Function.URL] == null)
                    {
                        hbFunction.Add(Function.URL, Function);
                    }
                    else
                    {
                        functionInfo = hbFunction[Function.URL] as DataContext.uspFunctionGetListByAdminIDForLoginResult;
                        permissionItem = functionInfo.Permission | Function.Permission;
                        functionInfo.Permission = permissionItem;
                    }
                }
                foreach (string key in hbFunction.Keys)
                {
                    functionInfo = hbFunction[key] as DataContext.uspFunctionGetListByAdminIDForLoginResult;
                    permission.Append(functionInfo.URL).Append("#").Append(functionInfo.Permission).Append("|");
                }
            }
            Program.UserEntity.Permission = permission.ToString();
        }
    }
}
